﻿import pyautogui
from PIL import Image, ImageFilter

import time, datetime

def mosaic_area(x, y, width, height, block_size=10):
    """
    对指定区域的桌面打马赛克
    :param x: 矩形区域左上角的 x 坐标
    :param y: 矩形区域左上角的 y 坐标
    :param width: 矩形区域的宽度
    :param height: 矩形区域的高度
    :param block_size: 马赛克块的大小
    """
    # 截取指定区域的屏幕
    screenshot = pyautogui.screenshot(region=(x, y, width, height))

    # 对截图进行马赛克处理
    for i in range(0, screenshot.width, block_size):
        for j in range(0, screenshot.height, block_size):
            box = (i, j, i + block_size, j + block_size)
            region = screenshot.crop(box)
            avg_color = region.resize((1, 1), Image.LANCZOS).getpixel((0, 0))
            region = Image.new('RGB', (block_size, block_size), avg_color)
            screenshot.paste(region, box)

    # 保存处理后的图片
    screenshot.save("mosaic_screenshot.png")
    print("已对指定区域打马赛克并保存为 mosaic_screenshot.png")

if __name__ == "__main__":
    # 定义特定区域，这里假设矩形区域左上角坐标为 (100, 100)，宽度为 200，高度为 200
    area_x = 0
    area_y = 0
    area_width = 83
    area_height = 1080

    time.sleep(8)

    mosaic_area(area_x, area_y, area_width, area_height)